﻿// Copyright (c) 2012 - 2020 Jared Zheng <jaredz at outlook dot com>
// Buddha Framework is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
//          http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.
//
// Create : 2012-12-01 version 0.1 Jared Zheng <jaredz at outlook dot com>
// Update : 2018-12-05 version 3.1.5 Jared Zheng <jaredz at outlook dot com>

#ifndef __COMMON_ROUTINE_H__
#define __COMMON_ROUTINE_H__

#pragma once

#include "object.h"

///////////////////////////////////////////////////////////////////////////////////////////////////////////
/// 服务器例程接口
class ICommonServer;
class NOVTABLE ICommonRoutine ABSTRACT : public CObject
{
public:
    /// 开启例程处理
    /// @param[in] ICommonServer* pServer例程处理的事件源服务器
    /// @return 成功返回true， 否则false
    virtual bool Start(ICommonServer* pServer) PURE;
    /// 暂停或者继续
    virtual bool Pause(bool bPause = true) PURE;
    /// 更新
    virtual bool Update(void) PURE;
    /// 停止
    virtual void Stop(void) PURE;

    /// 获取异步事件队列的工作线程数量
    virtual UInt     GetThread(void) PURE;
    /// 按服务器ID获取对应的socket
    /// 内部服务器无法直接和外网端通信, 需要通过层级一层一层将数据发送到网关才能最终到外网端
    /// 如果中间的服务器断线重连过了, 原来保存的socket值就没有如何用处, 所以需要通过唯一的ID
    /// 来获取可以使用的服务器socket, 才能将数据通过在线通道发到外网端
    /// 内部服务器保存中间服务器的socket使用GetAttr先验证socket是否有效再调用GetSocket
    /// 同时服务器往内部转发数据, USRVID保存服务器id用于通过id查找socket
    virtual Socket_t GetSocket(UInt uType, UInt uId) PURE;
    /// 设置框架异步事件队列工作线程超时检测的时间, 单位[S]
    virtual bool     SetTimeout(UInt uTimeout = 0) PURE;

    /// 加入例程队列
    virtual bool AddQueue(Int nEvent, uintptr_t utData = 0, ULLong ullParam = 0, QUEUE_TYPE eType = QUEUE_TYPE_NONE) PURE;
    virtual bool AddQueue(Int nEvent, CEventBase& EventRef, ULLong ullParam = 0, QUEUE_TYPE eType = QUEUE_TYPE_REFCOUNT) PURE;
    virtual bool AddQueue(Int nEvent, CStream& Stream, ULLong ullParam = 0) PURE;
};
typedef CTRefCountPtr<ICommonRoutine> CCommonRoutinePtr;

#endif // __COMMON_ROUTINE_H__
